#include <iostream>
#include <pthread.h>
#include <string>
#include <functional>
using namespace std;
namespace ThreadMoudle
{
    using func_t = function<void(string name)>;
    class thread
    {
    public:
        thread(const string name, func_t func)
            : _name(name), _func(func)
        {}

        void Execute()
        {
            cout << "name: " << _name << " is running..." << endl;
            _isrunning = true;
            _func(_name);
            _isrunning = false;
        }

        static void *ThreadRoutine(void *args)
        {
            thread *self = static_cast<thread *>(args);
            self->Execute();
            return nullptr;
        }
        bool start()
        {
            int n = pthread_create(&_tid, nullptr, ThreadRoutine, this);
            if (n != 0)
                return false;
            else
                return true;
        }

        void stop()
        {
            if (_isrunning)
            {
                pthread_cancel(_tid);
                _isrunning = false;
                cout << "name:" << _name << " stop..." << endl;
            }
        }
        
        void join()
        {
            pthread_join(_tid, nullptr);
            cout << "name:" << _name << " join..." << endl;
        }

    private:
        pthread_t _tid;
        string _name;
        bool _isrunning;
        func_t _func;
    };
}